var app = angular.module('app', ['app.tool','ui.router','oc.lazyLoad']);
// 配置app
var token,PERMISSION = {},isDirect,agencyID,userID;
// 配置白名单
app.config(function($sceDelegateProvider) {  
    $sceDelegateProvider.resourceUrlWhitelist([
        'self',
        'http://res1.hubcloud.com.cn/**',
        'https://res1.hubcloud.com.cn/**',
      ])
})

app.controller("indexCtrl",function($scope,proxy,$state,$location){
    $scope.$state = $state;
    // $scope.oem = JSON.parse(localStorage.oem);
    var parm = $location.url().match(/agent\/(\d+)\/(\d+)\//)
    $scope.type = parm[1];
    $scope.id = parm[2];
    agencyID = localStorage["advOrgID"+$scope.id];
    isDirect = localStorage["agbIsDirect"+agencyID];
    token = localStorage["advToken"+$scope.id]

    getUser();
    
    $scope.logout = function(){
        proxy.proxy({
            url: STATIC_URL.domian+'/dsp/user/logout',
            data: {
                loginName: $scope.loginName
            }
        })
        localStorage.token = "";
        location.href = "./login.html";
    }
    $scope.advUser = {};

    
    $scope.hover = false;
    $scope.getBalnce = function(){
        $scope.hover = !$scope.hover;
        getBalnce();
    }

    function getUser(){
        // 权限
        proxy.proxy({
            url: STATIC_URL.domian+'/dsp/pri/user/action',
            data: {},
            success: function(res){
                // 为PERMISSION对象添加属性  而不是直接将res.data赋值给PERMISSION
                // 可以防止在接口返回前使用PERMISSION导致拿不到权限内容
                PERMISSION.view = setData(res.data.view);
                PERMISSION.act = setData(res.data.act);
                function setData(arr) {
                    arr = arr || [];
                    for(var i = 0; i < arr.length; i++) {
                        var temp = arr[i];
                        arr[temp.name] = temp;

                        if(temp.sub) setData(temp.sub);
                    }
                    return arr;
                }
                $scope.permission = PERMISSION;
                // 向下广播
                $scope.$broadcast("permission", PERMISSION)


                 // 如果在初始页，根据权限跳转第一个页面
                 if(location.href.match(/agent\/(\d*)\/(\d*)\/$/i)) {
                    var view = [
                        {name: "首页",url: "agent.home"},
                        {name: "投放管理",url: "agent.delivery",hasSub:true},
                        {name: "报表中心",url: "agent.report",hasSub:true},
                        {name: "系统管理",url: "agent.manage",hasSub:true},
                        {name: "财务管理",url: "agent.finance",hasSub:true},
                    ]

                    for(var i = 0; i < view.length; i++) {
                        var v = view[i];
                        // 如果权限存在该页面，则跳转至该页面
                        if(PERMISSION.view[v.name]) {
                            // 延迟跳转，防止触发Transition Rejection错误
                            setTimeout(function(){
                                $state.go(v.url,{type:$scope.type,id:$scope.id});
                                // 如果有子级，则触发点击事件跳转第一个子级
                                if(v.hasSub) {
                                    $(".nav_list > li > a").eq(0).trigger("click")
                                }
                            },300)
                            break;
                        }
                    }
                }
            }
        })

        // 基本信息
        proxy.proxy({
            url: STATIC_URL.domian+'/dsp/UserInfo',
            data: {
                type: "adv"
            },
            success: function(res){
                // 获得用户状态
                $scope.advUser = res.data;
                //控制投放
                localStorage.advRights = res.data.advRights;
            }
        })

        
        getDepot();
    }

    function getBalnce() {
        $scope.finance = {
            balance:"--"
        }
        // 财务
        proxy.proxy({
            url: STATIC_URL.domian+'/dsp/advertiser/balance',
            data: {},
        }).then(function(res) {
            // 获得列表
            $scope.finance.balance = res.data;
        })
    }
    
    function getDepot() {
        proxy.proxy({
            url: STATIC_URL.domian+'/dsp/sys/parameter/query',
            data: {
                "filter":{"feature":"dsp.feature.device.depot.allow"},
            },
            success: function(res){
                $scope.hasDepot = res.data[0].value == 1;
            }
        })
    }

    $(".nav_dropdown").css("width",document.body.clientWidth);
    document.body.onresize = function(){
        $(".nav_dropdown").css("width",document.body.clientWidth);
    }
    $(document).on("click",".close-layer",function () {
        layer.closeAll();
    });
})


// 跳转第一个链接
$(document).on("click",".nav_list .toFirstSub",function(){
    $(this).siblings("ul").find("a:eq(0)")[0].click();
})